package FIle;

import java.io.*;

public class FileTest_InOutStream {
    public static void main(String[] args) {
        String source = "C:\\Users\\wj\\Desktop\\happy.txt";
        String target = "C:\\Users\\wj\\Desktop\\123.png";
        copyFile(source, target);


    }

    private static void copyFile(String source, String target) {
        // 字节流
        FileInputStream fi = null;
        FileOutputStream fo = null;

        BufferedInputStream bif = null;
        BufferedOutputStream bof = null;
        try {
            // 实例化FIle类
            File souFile = new File(source);
            File tarFile = new File(target);

            // 提供具体的流
            fi = new FileInputStream(souFile);
            fo = new FileOutputStream(tarFile);

            bif = new BufferedInputStream(fi);
            bof = new BufferedOutputStream(fo);


            // 赋值
            int len;
            byte[] cbuf = new byte[5];
            while ((len = bif.read(cbuf)) != -1){
                    bof.write(cbuf,0,len);
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if(fi != null)
                    fi.close();
                if(fo != null)
                    fo.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
